﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据表 LXK_11_ML_ProductList 对应的数据应用功能类。
	/// </summary>
	public class LXK_11_ML_ProductListApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private ILXK_11_ML_ProductListRepository service = new LXK_11_ML_ProductListRepository();

        public List<LXK_11_ML_ProductListEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<LXK_11_ML_ProductListEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FProductID == keyword);
            }
            return service.FindList(expression, pagination);
        }

        public List<LXK_11_ML_ProductListEntity> GetList()
        {
            var expression = ExtLinq.True<LXK_11_ML_ProductListEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            Pagination pagination = new Pagination();
            pagination.rows = 100000000;
            pagination.sidx = "FProductNO";
            pagination.sord = "ASC";
            return service.FindList(expression, pagination);
        }

        public LXK_11_ML_ProductListEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.FProductID == keyValue);
        }

        public void SubmitForm(LXK_11_ML_ProductListEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.FProductID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedEntity(LXK_11_ML_ProductListEntity entity)
        {
            var expression = ExtLinq.True<LXK_11_ML_ProductListEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductNO == entity.FProductNO);
            if (!string.IsNullOrEmpty(entity.FProductID))
            {
                expression = expression.And(t => t.FProductID != entity.FProductID);
            }
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }

        public int ExecPublish(string ids)
        {
            string sqlString = "UPDATE [LXK_11_ML_ProductList] SET [MaintainStatus]='1',FUpdateBy='" + OperatorProvider.Provider.GetCurrent().UserName + "',FUpdateDate='" + DateTime.Now.ToString("yyyy-MM-dd") + "',FUpdateTime='" + DateTime.Now.ToString("HH:mm:ss") + "' WHERE [MaintainStatus]='0' AND [FProductID] IN (" + ids + ")";
            return service.ExecSql(sqlString);
        }

        public int ExecCanMaintain(string ids)
        {
            string sqlString = "UPDATE [LXK_11_ML_ProductList] SET [FSign]='1',FUpdateBy='" + OperatorProvider.Provider.GetCurrent().UserName + "',FUpdateDate='" + DateTime.Now.ToString("yyyy-MM-dd") + "',FUpdateTime='" + DateTime.Now.ToString("HH:mm:ss") + "' WHERE [FSign]='0' AND [FProductID] IN (" + ids + ")";
            return service.ExecSql(sqlString);
        }

        public int ExecInsertContent(LXK_11_ML_ProductListEntity entity)
        {
            string ZCBH1 = "";
            string ZCBH2 = "";
            string ZCBH3 = "";
            string ZCBH4 = "";
            string ZCBH5 = "";
            if (entity != null && !string.IsNullOrEmpty(entity.FElseExplain))
            {
                string[] array = entity.FElseExplain.Split(',');
                if (array.Length > 0)
                { 
                    ZCBH1 = array[0];
                }
                if (array.Length > 1)
                {
                    ZCBH2 = array[1];
                }
                if (array.Length > 2)
                {
                    ZCBH3 = array[2];
                }
                if (array.Length > 3)
                {
                    ZCBH4 = array[3];
                }
                if (array.Length > 4)
                {
                    ZCBH5 = array[5];
                }
            }
            string sql = string.Format("EXEC Pro_LXGX_LJSC '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','A'", UseUnit, entity.FProductNO, entity.FProductName, entity.FFeatureExplain, entity.FElseExplain, entity.FProductClass, entity.FSign, ZCBH1, ZCBH2, ZCBH3, ZCBH4, ZCBH5, OperatorProvider.Provider.GetCurrent().UserName);
            return service.ExecSql(sql);
        }

        public int ExecUpdateContent(LXK_11_ML_ProductListEntity entity)
        {
            string ZCBH1 = "";
            string ZCBH2 = "";
            string ZCBH3 = "";
            string ZCBH4 = "";
            string ZCBH5 = "";
            if (entity != null && !string.IsNullOrEmpty(entity.FElseExplain))
            {
                string[] array = entity.FElseExplain.Split(',');
                if (array.Length > 0)
                {
                    ZCBH1 = array[0];
                }
                if (array.Length > 1)
                {
                    ZCBH2 = array[1];
                }
                if (array.Length > 2)
                {
                    ZCBH3 = array[2];
                }
                if (array.Length > 3)
                {
                    ZCBH4 = array[3];
                }
                if (array.Length > 4)
                {
                    ZCBH5 = array[5];
                }
            }
            string sql = string.Format("EXEC Pro_LXGX_LJSC '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','E'", UseUnit, entity.FProductNO, entity.FProductName, entity.FFeatureExplain, entity.FElseExplain, entity.FProductClass, entity.FSign, ZCBH1, ZCBH2, ZCBH3, ZCBH4, ZCBH5, OperatorProvider.Provider.GetCurrent().UserName);
            return service.ExecSql(sql);
        }
    }
}